package com.central.apps.handler;

import com.central.app.service.HandlerService;
import com.central.apps.handler.base.LvBaseHandler;
import com.central.common.web.PageResult;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Component
@Log4j2
public class personHandler extends LvBaseHandler {
    @Autowired
    private HandlerService handlerService;

//    @Override
//    public Object operate(String app, String type, Map<String, Object> params) throws Exception {
//
//        if("getWorkGroup".equals(type)){
//            String deptcode= MapUtils.getString(params,"deptcode");
//            String groupnum= BLuntil.getParameter(params,"groupnum");
//            String description= BLuntil.getParameter(params,"description");
//
//            String sql="select * from bl_workgroup where LEFT(deptcode,6)=LEFT('"+deptcode+"',6)  and groupnum  like '%"+groupnum+"%'  and description  like '%"+description+"%'";
//            return BLuntil.toPage(params,handlerService,sql);
//        }
//        return super.operate(app, type, params);
//    }

    @Override
    public void afterSave(String app, String objName, Map<String, Object> data, boolean isUpdate) throws Exception {
        if("personApp".equals(app)){
            //保存后不允许修改人员台账里的部门信息
            String id = MapUtils.getString(data, "id");
            handlerService.findBySql("update gq_person set astrict = '1' where id = '" + id + "'");
            //保存后不允许修改人员编号
            handlerService.findBySql("update gq_person set isnonew = '否' where id = '" + id + "'");
        }
        super.afterSave(app, objName, data, isUpdate);
    }

    @Override
    public String validateSave(String app, String objName, Map<String, Object> data) throws Exception {
        if("personApp".equals(app)){
            String id = MapUtils.getString(data, "id");
            String personid = MapUtils.getString(data, "personid");
            if(StringUtils.isBlank(id)) {
                List<Map<String, Object>> bySql = handlerService.findBySql("select * from gq_person where personid = '" + personid + "'");
                if (CollectionUtils.isNotEmpty(bySql)) {
                    return "人员编号" + personid + "在系统中已存在，请修改后再保存！";
                }
            }else{
                List<Map<String, Object>> bySql = handlerService.findBySql("select id from gq_person where personid = '" + personid + "'");
                if(CollectionUtils.isNotEmpty(bySql)){
                    String id1 = MapUtils.getString(bySql.get(0), "id");
                    if(!id.equals(id1)){
                        return "人员编号" + personid + "在系统中已存在，请修改后再保存！";
                    }
                }
            }
        }
        return super.validateSave(app, objName, data);
    }

    @Override
    public Object operate(String app, String type, Map<String, Object> params) throws Exception {
        if(app.equals("personApp")){
            if(type.equals("getPersonByDeptCode")){
                Integer limit = MapUtils.getInteger(params, "limit");
                Integer page = MapUtils.getInteger(params, "page");
                String code = MapUtils.getString(params, "code");
                String personid = MapUtils.getString(params, "personid");
                String name = MapUtils.getString(params, "name");
                String whereCondition = MapUtils.getString(params, "where_condition");//符合关系配置
                String sql ="select * from gq_person t where status = '活动'";
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.append(sql);
                if(StringUtils.isNotBlank(code)){
                    stringBuilder.append(" and t.deptcode like concat('%','"+code+"','%')");
                }
                if(StringUtils.isNotBlank(personid)){
                    stringBuilder.append(" and t.personid like concat('%','"+personid+"','%')");
                }
                if(StringUtils.isNotBlank(name)){
                    stringBuilder.append(" and t.name like concat('%','"+name+"','%')");
                }
                if(StringUtils.isNotEmpty(whereCondition)){
                    stringBuilder.append(" and ").append(whereCondition);
                }


                PageResult<Map<String, Object>> mapPageResult = handlerService.pageList(page == null ? 1 : page, limit == null ? 10 : limit, stringBuilder.toString());
                return mapPageResult;
            }
        }
        return super.operate(app, type, params);
    }
}
